/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package modele;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;
import java.util.HashSet;
import java.util.TreeSet;

/**
 *
 * @author ighembaza
 */
public class DAO {

    public static Collection<Matiere> getLesMatieres() {
        Collection<Matiere> lesMatieres = new TreeSet<Matiere>();
        try {
            Connection con = Connect.get();
            Statement req;
            req = con.createStatement();
            ResultSet rs = req.executeQuery("select id,nom from matiere");

            //Parcours
            String id;
            String nom;
            TreeSet<Fichier> f;
            int i = 0;
            while (rs.next()) {
                id = rs.getString(1);
                nom = rs.getString(2);
                f = new TreeSet<Fichier>();
                Statement reqF = con.createStatement();
                ResultSet rsP = reqF.executeQuery(
                        "select id, nom "
                        + "from fichier "
                        + "where idMat = '" + id + "'");
                while (rsP.next()) {
                    f.add(new Fichier(rsP.getString(1), rsP.getString(2)));

                }
                rsP.close();

                lesMatieres.add(new Matiere(id, nom, f));
                i++;
            }
            rs.close();

        } catch (SQLException e) {
            System.out.println("Erreur SQL :" + e);
        }
        return lesMatieres;
    }

}
